<?php

require_once('config.php');
Doctrine::loadModels('models');

$module = isset($_REQUEST['module']) ? $_REQUEST['module']:'People';
$action = isset($_REQUEST['action']) ? $_REQUEST['action']:'list';

if ($module == 'People') {
    $itemId = isset($_REQUEST['item_id']) && $_REQUEST['item_id'] > 0 ? $_REQUEST['item_id']:null;
    $userTable = Doctrine::getTable('People');

    if ($itemId === null) {
        $item = new People();
    } else {
        $item = $userTable->find($itemId);
    }

    switch ($action) {
        case 'edit':
        case 'add':
            echo '<form action="index.php?module=LkGlossary&action=save" method="POST">
                  <fieldset>
                    <legend>Item</legend>
                    <input type="hidden" name="id" value="' . $item->agent_person_id . '" />
                    <label for="mobile">Mobile</label> <input type="text" name="item[mobile]" value="' . $item->mobile . '" />
                    <label for="email">Email</label> <input type="text" name="item[email]" value="' . $item->email . '" />
                    <input type="submit" name="save" value="Save" />
                  </fieldset
                  </form>';
            break;
        case 'save':
            $user->merge($_REQUEST['item']);
            $user->save();

            header('location: peopleCrud.php?module=People&action=edit&id=' . $user->agent_person_id);
            break;
        case 'delete':
            $user->delete();

            header('location: peopleCrud.php?module=People&action=list');
            break;
        default:
            $query = new Doctrine_Query();
            $query->from('People i')
                  ->orderby('i.email');

            $items = $query->execute();

            echo '<ul>';
            foreach ($items as $item) {
                echo '<li><a href="peopleCrud.php?module=People&action=edit&id=' . $item->agent_person_id . '">' . $item->email . '</a> &nbsp; <a href="peopleCrud.php?module=People&action=delete&id=' . $item->agent_person_id . '">[X]</a></li>';
            }
            echo '</ul>';
    }

    echo '<ul>
            <li><a href="peopleCrud.php?module=People&action=add">Add</a></li>
            <li><a href="peopleCrud.php?module=People&action=list">List</a></li>
          </ul>';
} else {
    throw new Exception('Invalid module');
}

?>
